﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery tabs
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.实现静态html结构转换以及数据源绑定两种方式
2.支持固定内容、目标内容，Ajax读取内容
3.支持多区域和单一区域
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region 设置
    var tabSetting = {
        css: 'tabs'
            , csscontent: 'tab-content'
            , csscontentitem: 'tab-content-item'
    };
    ///#endregion

    $.fn.tabs = function (contents, datas, options) {
        /// <summary>pager</summary>
        /// <param name="contents" type="JQSelector">Tab内容区域</param>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>               
        options = $.extend({
            tabs: iconfig.tabs.container
            , item: iconfig.tabs.item
            , ajaxcontent: null
            , selectIndex: 0
            //            , isreload:false
        }, options);
        var tabcontainer = this;
        if (datas && datas.length > 0) {
            var tabs = $(options.tabs).addClass(tabSetting.css).appendTo(tabcontainer);
            ictrcore.teml(tabs, options.item, datas)
                .each(function (i) {
                    setTabs($(this).children('a'), i);
                });
        }
        else
            setTabs(tabcontainer.find('ul.' + tabSetting.css + ' > li > a'));

        $(contents).addClass(tabSetting.csscontent);
        if (hasValue(options.selectIndex))
            $.tabSelect('#' + tabcontainer.attr('id'), options.selectIndex);
        ///#endregion

        function setTabs(items, index) {
            var ajaxcontent = options.ajaxcontent;
            items.each(function (i) {
                $(this).data('index', index || i).click(function () {
                    var link = $(this), tab = $(this).parent('li');
                    tab.siblings('.selected').removeClass('selected');
                    tab.addClass('selected');
                    $(contents + ' .' + tabSetting.csscontentitem + ':visible').hide();

                    ///#region load tab 内容
                    if (!link.data('isloaded')) {
                        var targetid = link.attr('href'), ui = link.attr('ui'), target;
                        if (strNotEmpty(ui) && (!strNotEmpty(targetid) || targetid === '#') && ajaxcontent)
                            targetid = ajaxcontent;

                        if (strNotEmpty(targetid) && targetid !== '#')
                            target = $(contents + ' ' + targetid);
                        else {
                            var index = link.data('index');
                            if (hasValue(index))
                                target = $(contents + ' .' + tabSetting.csscontentitem + ':eq(' + index + ')');
                        }
                        if (target && target.length > 0) {
                            if (ui)//优先处理ajax
                                ictrcore.loadPage(ui, target);
                            target.show();
                        }
                        link.data('isloaded', true);
                    }
                    else {
                        $(contents + ' .' + tabSetting.csscontentitem + ':eq(' + link.data('index') + ')').css('display','block');
                    }
                    ///#endregion
                });
            });

        }
    };

    ///#region 公共方法
    $.tabSelect = function (ctr, index) {
        $(ctr + ' ul.' + tabSetting.css + ' li:eq(' + index + ') a').click();
    }
    ///#endregion

})(jQuery);